package com.zyn.rsgl.controller;


import com.zyn.rsgl.pojo.Recruitment;
import com.zyn.rsgl.service.RecruitmentService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 招聘表 前端控制器
 * </p>
 *
 * @author zcs
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/rsgl/recruitment")
public class RecruitmentController {
    @Resource
    private RecruitmentService recruitmentService;

    // 分页查询
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String positionName,
            @RequestParam(required = false) String interviewResult) {
        return recruitmentService.listPage(pageNum, pageSize, positionName, interviewResult);
    }

    // 获取单个
    @GetMapping("/get/{recruitmentId}")
    public R get(@PathVariable("recruitmentId") String recruitmentId) {
        Recruitment byId = recruitmentService.getById(recruitmentId);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("未找到该招聘信息");
    }

    // 添加
    @PostMapping("/add")
    public R add(@RequestBody Recruitment recruitment) {
        // 判断是否存在
        if (recruitmentService.getById(recruitment.getId()) != null) {
            return R.fail("招聘信息已存在");
        }
        return recruitmentService.save(recruitment) ? R.ok() : R.fail("添加招聘信息失败");
    }

    // 修改
    @PutMapping("/update")
    public R update(@RequestBody Recruitment recruitment) {
        return recruitmentService.updateById(recruitment) ? R.ok() : R.fail("修改招聘信息失败");
    }

    // 删除
    @DeleteMapping("/delete/{recruitmentId}")
    public R delete(@PathVariable("recruitmentId") String recruitmentId) {
        return recruitmentService.removeById(recruitmentId) ? R.ok() : R.fail("删除招聘信息失败");
    }
}
